<?php


namespace app\model;

use think\Model;

class Goods extends Model
{
    public function getGoods(){


        $all = Goods::where('status',1)->where('stock','>',0)->paginate(10);
        return $all;
    }

    public function saveOne(){
        Goods::save();
    }
    public function createOne(){
        Goods::create();
    }
    public function getStatusAttr($v){
        $status=[
            1=>'在售',
            0=>'下架'
        ];
        return $status[$v];
    }

    public function updateStock($id){
        Goods::where('id',$id)->dec('stock',1)->update();
    }

    public function recovery($id,$quantity){
        Goods::where('id',$id)->field('stock')->inc('stock',$quantity)->update();
    }

    //增加商品的库存
    public function addAll(){
        Goods::where('id','<>',0)->field('stock')->inc('stock',47)->update();
    }

    //取消订单后商品数量恢复相应的值
    public function cancel($goods_id,$quantity){
        $res = Goods::where('id',$goods_id)->inc('stock',$quantity)->update();
        return $res;
    }

    //获取所有商品的id
    public function getAllId(){
        return Goods::where('id','<>',-1)->field('id')->field('price')->select();
    }

}